package ntd.util.frame.javacv.action;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.StaticLog;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.util.frame.data.factory.FactoryData;

import java.sql.SQLException;
import java.util.List;

/**
 * 数据质量
 */
public class ActionDataQuality extends ActionAbs {

    @Override
    public ActionInfo info() {
        return new ActionInfo("机器视觉数据质量修正", "ai-vision-data-quality-fix", new ActionDataQuality());
    }

    private void del(Db db, Entity entity) throws SQLException {
        int del = db.del(entity);
        StaticLog.info("数据{}删除 - {}", entity.getStr("id"), del);
    }

    public static void main(String[] args) {
        new ActionDataQuality().execute(null);
    }

    @Override
    public ActionOutBo handle() {
        Db db = Db.use(FactoryData.dataSource("272ab1b9c5cc4ea2b76302238247284c"));

        List<Entity> entityList = null;
        try {
            entityList = db.query("select * from airesult ORDER BY create_time DESC limit 500");

            for (Entity entity : entityList) {
                String imageUrl = entity.getStr("imageUrl");
                try {
                    byte[] bytes = HttpUtil.downloadBytes(imageUrl);
                    if (ArrayUtil.isEmpty(bytes)) {
                        del(db, entity);
                    }
                } catch (Exception e) {
                    del(db, entity);
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return new ActionOutBo();
    }
}
